package com.slj.springcloud.service.impl;

import com.slj.springcloud.client.PayClient;
import com.slj.springcloud.model.response.Result;
import com.slj.springcloud.model.response.ResultCode;
import com.slj.springcloud.service.ResilienceService;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ResilienceServiceImpl implements ResilienceService {
    @Autowired
    private PayClient payClient;

    @Override
    @CircuitBreaker(name = "cloud-pay", fallbackMethod = "myCircuit")
    public String circuit(Long id) {
        Result<String> result = payClient.circuit(id);
        log.info("调用了远程API");
        if (result.getCode() != ResultCode.SUCCESS.getCode()) {
            throw new RuntimeException("访问错误pay错误");
        }
        return payClient.circuit(id).getData();
    }

    private String myCircuit(Long id, Throwable t) {
        log.info("id={},t={}", id, t.getMessage());
        return "容错处理，返回OK";
    }
}
